#include	"../network/LoginService.h"
#include	"../network/CenterConnector.h"

#include	<protocols/Common.pb.h>
#include	<Crypto.h>
#include	<Json.h>

void VerifySimple(Sender * sender, const std::string & token, uint32_t sid, const std::string & code, uint32_t serial) {
	static const std::string secret = "SIMPLE_SECRET_DEVELOP{7836C2A8-D67E-4B5F-981A-5E70570EE2B3}";

	Json::Value param;
	Json::Reader reader;

	if (!reader.parse(token, param) || !param.isObject() || !param["account"].isString() || !param["sign"].isString()) {
		LOG_ERR("[login]Verify failed. Simple token error : %s", token.data());
		LoginService::Instance().FailedVerify(sender, Proto::S_EBadParam, serial);
	} else {
		std::string account = param["account"].asString();

		MD5 md5;
		md5.Update(account);
		md5.Update(secret);

		if (md5.ToString() != param["sign"].asString()) {
			LOG_ERR("[login]Verify failed. Simple token error MD5 : %s", token.data());
			LoginService::Instance().FailedVerify(sender, Proto::S_EBadParam, serial);
		} else {
			LoginService::Instance().Link(account, sender);
			CenterConnector::Instance().Verify(account, sid, code, serial);
		}
	}
}